package org.UserHouse.Servlet;

import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.UserHouse.Dao.Imp.UserDaoImp;
import org.UserHouse.Dao.UserDao;
import org.UserHouse.entity.User;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author bin
 * @date 2024-11-26
 */
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        String Uname = req.getParameter("Uname");
        String Upassword = req.getParameter("Upassword");
        String Utype = req.getParameter("Utype");

        UserDao userDao = new UserDaoImp();
        User user = userDao.selectByUname(Uname);

        String status = "";
        Gson gson = new Gson();


        if (user != null && Uname.equals(user.getUname()) && Utype.equals(user.getUtype())) {
            if (Upassword.equals(user.getUpassword())) {
                status = "success";
            } else {
                status = "error";
            }
        } else {
            status = "error2";
        }
        String json = gson.toJson(status);
        out.println(json);
        out.flush();
        out.close();

    }
}
